# -*- coding: utf-8 -*-
class Privilege < ActiveRecord::Base
  #这个权限表只是用来分类的，给细节权限分类并无大的权限指导作用。
  has_many :privilege_details  ,:dependent => :destroy  #级联删除子表
  has_many :roles_privilege_details
  has_many :roles , :through =>:roles_privilege_details #这个暂时没用。
  belongs_to :parent ,:class_name =>'Privilege' ,:foreign_key =>'parent_id'
  has_many :children ,:class_name =>'Privilege' ,:foreign_key =>'parent_id'

  belongs_to :sys_controller
  belongs_to :sys_action

  def self.getUnlockedSysPrivilegeDetailsForRole(privilegeId)
    sysPrivilege = Privilege.first(:conditions=>["id = ? AND is_locked = 0",privilegeId])
    privilegeDetails = nil
    if sysPrivilege != nil
      #如果为0，则从新添加所有权限
      privilegeDetails = PrivilegeDetail.all(:conditions=>["privilege_id = ?",privilegeId])
    end
    privilegeDetails
  end
end
